import { Component, OnInit, Input } from '@angular/core';
import { SNode, MoveType, NodeAnd, NodeData } from 'src/app/module';
import { SvgService } from 'src/app/svgdom/svg.service';

@Component({
  selector: 'mask',
  templateUrl: './mask.component.html',
  styleUrls: ['./mask.component.scss']
})
export class MaskComponent implements OnInit {
  @Input() dataSourcs: SNode;

  // 将移动的点加入树结构，通过判断server内是否有保存新增的条件condition来区分是新增还是移动
  // 参数配置：0-插入节点上方，1插入节点下方，2生成下一级，并和被插入的节点形成兄弟节点
  MovingSNodeIntoTree(intoType: number) {
    if (this.ser.MovingData.MoveSNode === this.dataSourcs) {
      // up在自身不操作
      return;
    }
    switch (intoType) {
      case 0:
        const PNode = this.ser.MovingData.MoveSNode.Parent;
        this.moveSNodeOperation(this.ser.MovingData.AddOrMove, this.ser.MovingData.MoveSNode, this.dataSourcs, 0);
        if (!this.ser.MovingData.AddOrMove) {
          // 不是新增就需要检查删除
          this.deleteOrphanNode(PNode);
        }

        break;
      case 1:
        const PANode = this.ser.MovingData.MoveSNode.Parent;
        this.moveSNodeOperation(this.ser.MovingData.AddOrMove, this.ser.MovingData.MoveSNode, this.dataSourcs, 1);
        if (!this.ser.MovingData.AddOrMove) {
          // 不是新增就需要检查删除
          this.deleteOrphanNode(PANode);
        }
        break;
      default:
        const PSNode = this.ser.MovingData.MoveSNode.Parent;
        if (this.dataSourcs.Parent.Children.length === 1) {
          this.moveSNodeOperation(this.ser.MovingData.AddOrMove, this.ser.MovingData.MoveSNode, this.dataSourcs, 0);
        } else {
          this.generateBrotherNode(this.ser.MovingData.MoveSNode, this.dataSourcs);
        }
        if (!this.ser.MovingData.AddOrMove) {
          // 不是新增就需要检查删除
          this.deleteOrphanNode(PSNode);
        }
        break;
    }
    this.dataSourcs.Data.IsMarkDisplay = false;
  }
  // 新生成下一级，形成兄弟节点
  generateBrotherNode(serverNode, sourceNode: SNode) {
    if (serverNode.Parent) {
      serverNode.Delete();
    }
    const idx = sourceNode.Parent.Children.indexOf(sourceNode);
    const ds: SNode = new SNode();
    const dt: NodeData = {
      Conditions: sourceNode.Data.Conditions,
      ColumnNames: sourceNode.Data.ColumnNames, // 列的所有名称，用来弹出框中的下拉选项
      IsSvgDisplay: true, // svg线条是否显示,true为显示
      IsDisplay: true, // 除线条之外是否显示
      IsExistele: false, // 该点是否有收缩，true为有
      IsMarkDisplay: false // 遮罩层是否显示，true为显示
    };
    ds.Data = dt;
    ds.NodeType = NodeAnd;
    ds.Parent = sourceNode.Parent;
    ds.Status = 'n';

    sourceNode.Parent.AddChildNode(ds, idx + 1); // 0为起始
    sourceNode.Delete(); // 一定要新增完再删除，不然找不到他的父节点
    ds.Children.push(sourceNode);
    ds.Children.push(serverNode);
    sourceNode.Parent = ds;
    serverNode.Parent = ds;
  }
  // 操作节点进入树，isAdd判断是否是移动，serverNode服务中保存的节点，sourceNode数据源点，upORdown插入位置上还是下
  moveSNodeOperation(isAdd: boolean, serverNode, sourceNode: SNode, upORdown: number) {
    if (!isAdd) {
      // 移动需要删除原来节点
      serverNode.Delete();
    }
    serverNode.Parent = sourceNode.Parent;
    const idx = sourceNode.Parent.Children.indexOf(sourceNode);
    sourceNode.Parent.Children.splice(idx + upORdown, 0, serverNode);
  }
  // 删除只有一个孩子的连接节点,这个应该改成向上冒泡
  deleteOrphanNode(sourceNode: SNode) {
    if (sourceNode.Parent && sourceNode.Children.length === 1) {
      const idx = sourceNode.Parent.Children.indexOf(sourceNode.Parent);
      sourceNode.Parent.Children.splice(idx, 0, sourceNode.Children[0]);
      sourceNode.Children[0].Parent = sourceNode.Parent;
      sourceNode.Delete();
    }
  }
  constructor(private ser: SvgService) {}

  ngOnInit() {}
}
